﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ClassModel;
using System.Data.SqlClient;

namespace ClassDAL
{
    public class t_class_grade_DAL
    {
        /// <summary>
        /// 插入年级信息
        /// </summary>
        /// <param name="grade"></param>
        /// <returns></returns>
        public static int InsertGradeInfo(t_class_grade grade)
        {
            string insertSQL = "insert into t_class_grade values(@gradename,@fileid,@createtime);select @@identity;";
            SqlParameter[] insertParams = new SqlParameter[] 
            {
                new SqlParameter("@gradename",grade.gradename),
                new SqlParameter("@fileid",grade.fileid),
                new SqlParameter("@createtime",grade.createtime)
            };


            return Convert.ToInt32(SQLHelper.ExecuteScalar(SQLHelper.GetConnection(), System.Data.CommandType.Text,insertSQL,insertParams));
        }



        /// <summary>
        /// 根据 excelid 获取年纪信息
        /// </summary>
        /// <param name="fileid"></param>
        /// <returns></returns>
        public static t_class_grade GetGradeInfoWithFileId(int fileid)
        {
            string selectSQL = "select id,gradename,fileid,createtime from t_class_grade where fileid = @fileid;";
            SqlParameter selectParam = new SqlParameter()
            {
                ParameterName = "@fileid",
                Value = fileid
            };

            SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.GetConnection(), System.Data.CommandType.Text, selectSQL, selectParam);
            if (!reader.Read()) return null;

            t_class_grade grade = new t_class_grade();
            grade.id = Convert.ToInt32(reader["id"]);
            grade.gradename = reader["gradename"] == DBNull.Value ? "unknow" : reader["gradename"].ToString();
            grade.fileid = Convert.ToInt32(reader["fileid"]);
            grade.createtime = Convert.ToDateTime(reader["createtime"]);

            reader.Close();

            return grade;
        }
    }
}
